import java.util.HashMap;
import java.util.Map;

public class Main {
    public static void main(String[] args) {
        Solution solution = new Solution();
        int ret = solution.romanToInt("MCMXCIV");
        System.out.println(ret);
    }
}


class Solution {
    public int romanToInt(String s) {
        Map<Character, Integer> romanIntMap = new HashMap<>();
        romanIntMap.put('I', 1);
        romanIntMap.put('V', 5);
        romanIntMap.put('X', 10);
        romanIntMap.put('L', 50);
        romanIntMap.put('C', 100);
        romanIntMap.put('D', 500);
        romanIntMap.put('M', 1000);

        int total = 0;
        int curValue = 1;
        for(int i = s.length() - 1; i >= 0; i--){
            char c = s.charAt(i);
            int value = romanIntMap.get(c);
            if(value < curValue){
                total -= value;
                continue;
            }
            if(value > curValue){
                curValue = value;
            }
            total += value;
        }
        return total;
    }
}